package com.hnevc.acountms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.hnevc.acountms.R;
import com.hnevc.acountms.model.Tb_flag;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShowInfoActivity extends AppCompatActivity {
    public static final String IN_ACCOUNT = "InAccount";
    public static final String OUT_ACCOUNT = "OutAccount";
    Button mBtnOutInfo;
    Button mBtnInInfo;
    Button mBtnFlagInfo;

    ListView mLvFlagInfos;
    private List<Map<String, String>> mDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_info);
        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnInInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TotalChartActivity.actionStart(ShowInfoActivity.this, IN_ACCOUNT);
            }
        });
        mBtnOutInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TotalChartActivity.actionStart(ShowInfoActivity.this, OUT_ACCOUNT);
            }
        });
        mBtnFlagInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Tb_flag> flags = DataSupport.findAll(Tb_flag.class);
                mDataList = new ArrayList<>();
                for (int i = 0; i <flags.size() ; i++) {
                    HashMap<String,String> map = new HashMap<String, String>();
                    map.put("id",flags.get(i).getId()+"");
                    map.put("flag",flags.get(i).getFlag());
                    mDataList.add(map);
                }

                SimpleAdapter adpter = new SimpleAdapter(ShowInfoActivity.this, mDataList,
                        R.layout.flag_list_item,new String[]{"id","flag"}
                        ,new int[]{R.id.id_tv_id,R.id.id_tv_flag});
                mLvFlagInfos.setAdapter(adpter);
                mLvFlagInfos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        FlagManageActivity.actionStart(ShowInfoActivity.this
                                , Integer.parseInt(((TextView)view.findViewById(R.id.id_tv_id)).getText().toString()));
                    }
                });

            }
        });
    }

    private void initView() {
        mBtnOutInfo = (Button) findViewById(R.id.id_btn_outInfo);
        mBtnInInfo = (Button)findViewById(R.id.id_btn_inInfo);
        mBtnFlagInfo = (Button) findViewById(R.id.id_btn_flagInfo);
        mLvFlagInfos = (ListView) findViewById(R.id.id_lv_flagInfos);
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context,ShowInfoActivity.class);
        context.startActivity(intent);

    }
}
